<?php
require_once 'client'.EXT;

class Customer extends Client{

	function __construct()
	{
		parent::__construct();
		$this->load->model(array('customer_model','country_model','user_model'));
		$this->data['countries']	=	$this->country_model->get_all();
	}
	
	function index()
	{
		if($_SESSION['_user']['group_id'] !=1)
			$agents	=	$this->user_model->get_many_by('group_id',2);
		else 
			$agents	=	$this->user_model->get_all();
		$this->data['agents']	=	$agents;
		$page	=	(isset($_GET['p']) ? $_GET['p'] : 1);
		$limit	=	10;
		$offset	=	($page-1)*$limit;
		$where	=	array();
		if(isset($_GET['name']) && $_GET['name'] != '') $where['fullname']	=	$_GET['name'];
		if(isset($_GET['passport'])) $where["passport_no"]	=	$_GET['passport'];
		if(isset($_GET['agent']) && $_GET['agent'] != '') $where["create_by"]	=	$_GET['agent'];
		$this->data['tpl_file']		=	'customer/index';
		$result		=	$this->customer_model->get_limit($limit,$offset,$where);
		$this->data['customers']	=	$result['rows'];
		$conf	=	array(
			'base_url'		=>	current_link(),
			'cur_page'		=>	$page,	
			'total_rows'	=>	$result['num_rows'],
			'per_page'		=>	$limit
		);
		$this->pagination->initialize($conf);
		$this->data['pagnav']	=	$this->pagination->display_query_string();
		
		$this->load->view('layout/default', $this->data);
	}
	
	function add()
	{
		$this->data['module_title']	=	'Personal Particulars';
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$this->form_validation->set_rules('fullname','FullName','required');
			$this->form_validation->set_rules('passport','NRIC/FIN/Passport No.','required|callback__check_passport_no');
			$this->form_validation->set_rules('birth','Date of Birth','callback__check_date');
			$nationality	=	($this->input->post('other_nationality') ? $this->country_model->insert(array('name' => $this->input->post('other_nationality'))) : $this->input->post('nationatily'));
			//$this->form_validation->set_message('required','%s is ....');
			if($this->form_validation->run() === TRUE){
				$data	=	array(
					'title'				=>	$this->input->post('title'),
					'fullname'			=>	$this->input->post('fullname'),
					'nationality'		=>	$nationality,
					'passport_no'		=>	$this->input->post('passport'),
					'marital_status'	=>	$this->input->post('marital'),
					'date_of_birth'		=>	convert_date_mysql($this->input->post('birth')),
					'smoker'			=>	$this->input->post('smoker'),
					'occupation'		=>	$this->input->post('occupation'),
					'date_create'		=>	date('Y-m-d H:i:s'),
					'create_by'			=>	$_SESSION['_user']['user_id']
				);
				
				if($this->customer_model->insert($data)){
					redirect(base_url().'customer/');
				}
			}
			
		}
		$this->load->view('customer/add', $this->data);
	}
	
	function edit($id = NULL)
	{
		$this->data['customer']		=	$this->customer_model->get($id);
		$this->data['module_title']	=	'Personal Particulars';
		
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			//print_r($_POST);die;
			$this->form_validation->set_rules('fullname','FullName','required');
			$this->form_validation->set_rules('passport','NRIC/FIN/Passport No.','required');
			//$this->form_validation->set_rules('birth','Date of Birth','callback__check_date');
			if($this->input->post('other_nationality') && $this->input->post('other_nationality') != '')
			{
				$arr	=	array('name' => $this->input->post('other_nationality'));
				$nationality	=	$this->country_model->insert($arr);
			}
			else{
				$nationality	=	$this->input->post('nationatily');
			}
			//echo $this->input->post('other_nationality').$nationality;die;
			//$this->form_validation->set_message('required','%s is ....');
			if($this->form_validation->run() === TRUE){
				$data	=	array(
					'title'				=>	$this->input->post('title'),
					'fullname'			=>	$this->input->post('fullname'),
					'nationality'		=>	$nationality,
					'passport_no'		=>	$this->input->post('passport'),
					'marital_status'	=>	$this->input->post('marital'),
					'date_of_birth'		=>	convert_date_mysql($this->input->post('birth')),
					'smoker'			=>	$this->input->post('smoker'),
					'occupation'		=>	$this->input->post('occupation')
				);
				
				if($this->customer_model->update($id,$data)){
					redirect(base_url().'customer/');
				}
			}
			
		}
		
		$this->load->view('customer/edit', $this->data);
	}
	
	function del($id = NULL)
	{
		if($id)$this->customer_model->delete($id);
		redirect(base_url().'customer/');
	}
	
	function select($id = NULL)
	{
		//$_SESSION['customer']['id']	=	$id;
		$customer = $this->customer_model->get_by_id_array($id);
		$_SESSION['customer']	=	$customer;
        unset($_SESSION['data']);
        //$this->load->model('existing_coverage_model');
        //load existing coverage data from database by customer id
        //$row    =    $this->existing_coverage_model->get_by('customer_id',$id);
        //parse from row to array
		/*
        if($_SERVER['REQUEST_METHOD'] == 'GET' && $row)
        {
            $data    =    array(
                "id" => $row->id,
                "customer_id" => $row->customer_id,
                "policy_incepted" => $row->policy_incepted,
                "policy_term" => $row->policy_term,
                "sum_assured_dead" => $row->sum_assured_dead,
                "sum_assured_disability" => $row->sum_assured_disability,
                "sum_assured_critical_illness" => $row->sum_assured_critical_illness,
                "maturity_year" => $row->maturity_year,
                "maturity_value" => $row->maturity_value,
                "decreasing" => $row->decreasing,
                "consider_cash_value_for_retirement" => $row->consider_cash_value_for_retirement,
                "retirement_age" => $row->retirement_age,
                "cva_retirement" => $row->cva_retirement,
                "expected_annual_return" => $row->expected_annual_return,
                "step_up_booster" => $row->step_up_booster,
                "year_start" => $row->year_start,
                "year_end" => $row->year_end,
                "addiction_sum_assured_for_death" => $row->addiction_sum_assured_for_death,
                "addiction_sum_assured_for_disability" => $row->addiction_sum_assured_for_disability,
                "addiction_sum_assured_for_critical_illiness" => $row->addiction_sum_assured_for_critical_illiness,
                "company" => $row->company,
                "explanation_endownment" => $row->explanation_endownment,
                "explanation_term" => $row->explanation_term,       
                "explanation_ilp" => $row->explanation_ilp,
                "explanation_wl" => $row->explanation_wl,
                "explanation_hs" => $row->explanation_hs
            );
            
            $_SESSION["existing_coverage_data"] = $data;
        }*/
        
		redirect(base_url().'data_form.html');
	}
	
	function _check_passport_no($passport_no)
	{
		$row	=	$this->customer_model->get_by('passport_no',$passport_no);
		if($row){
			$this->form_validation->set_message('_check_passport_no','The %s is existed!');
			return FALSE;
		}
		return TRUE;
	}
	
	function _check_date($date)
	{
		if(!preg_match('/^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/(19|20)[0-9]{2}$/', $date, $matches))
	    {
	    	$this->form_validation->set_message('_check_date','The %s is invalid!');
	        return false;
	    } 
	    return true;
	}
}